unit CountryForm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, DBModule, Vcl.Grids, Vcl.DBGrids, IB,
  Vcl.ExtCtrls, Vcl.DBCtrls, Vcl.StdCtrls, Vcl.Mask, IBSQL, UserUtils, Data.DB,
  IBCustomDataSet, IBTable, IBDatabase;

type
  TfmCountry = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    DBGrid1: TDBGrid;
    btnClose: TButton;
    txtCountry: TEdit;
    txtAbbr: TEdit;
    btnNew: TButton;
    btnSave: TButton;
    btnDelete: TButton;
    btnRefresh: TButton;
    IBDatabase1: TIBDatabase;
    IBTransaction1: TIBTransaction;
    fbCountry: TIBTable;
    dsCountry: TDataSource;
    procedure btnCloseClick(Sender: TObject);
    procedure btnNewClick(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
    procedure DBGrid1DblClick(Sender: TObject);
    procedure btnDeleteClick(Sender: TObject);
    procedure btnRefreshClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure DBGrid1TitleClick(Column: TColumn);
  private
    { Private declarations }
    Operation: TOperation;
  public
    { Public declarations }
  end;

var
  fmCountry: TfmCountry;

implementation

{$R *.dfm}

procedure TfmCountry.btnCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfmCountry.btnDeleteClick(Sender: TObject);
begin
  try
    with DataModule1.qrySQL do
    begin
      SQL.Clear;
      Params.Clear;
      try
        SQL.Add('delete from Country where ID=:ID');
        Params.ParamByName('ID').AsString := DBGrid1.DataSource.DataSet.FieldByName('ID').AsString;
        ExecSQL;
        Transaction.Commit;
      except
        on E: EIBInterBaseError do
        begin
          Transaction.Rollback;
          ShowMessage(Format('IBErrorCode=%d, SQLCode=%d, Msg=%s, StackTrace=%s', [E.IBErrorCode, E.SQLCode, E.Message, E.StackTrace]));
        end;
      end;
    end;
  finally
    btnRefresh.Click;
  end;
end;

procedure TfmCountry.btnNewClick(Sender: TObject);
begin
  txtAbbr.Text := '';
  txtCountry.Text := '';
  Operation := opNew;
end;

procedure TfmCountry.btnRefreshClick(Sender: TObject);
begin
//  RefreshCountryStateCityDB;
  IBDatabase1.DatabaseName := DataModule1.IBDatabase1.DatabaseName;
  IBDatabase1.Connected := False;
  IBDatabase1.Connected := True;
  IBTransaction1.Active := True;
  with fbCountry do
  begin
    if not Active then
      Open;
    Last;
    First;
  end;
end;

procedure TfmCountry.btnSaveClick(Sender: TObject);
begin
  if (txtCountry.Text <> '') and (txtAbbr.Text <> '') then
    try
      with DataModule1.qrySQL do
      begin
        SQL.Clear;
        Params.Clear;
        try
          case Operation of
            opNew: begin
              SQL.Add('insert into Country (abbriviation, country) values (:CountryAbbr, :Country)');
            end;
            opEdit: begin
              SQL.Add('update Country set abbriviation=:CountryAbbr, country=:Country where ID=:ID');
              Params.ParamByName('ID').AsString := DBGrid1.DataSource.DataSet.FieldByName('ID').AsString;
            end;
          end;
          Params.ParamByName('CountryAbbr').AsString := txtAbbr.Text;
          Params.ParamByName('Country').AsString := txtCountry.Text;
          ExecSQL;
          Transaction.Commit;
        except
          on E: EIBInterBaseError do
          begin
            Transaction.Rollback;
            ShowMessage(Format('IBErrorCode=%d, SQLCode=%d, Msg=%s, StackTrace=%s', [E.IBErrorCode, E.SQLCode, E.Message, E.StackTrace]));
          end;
        end;
      end;
    finally
      btnRefresh.Click;
      btnNew.Click;
    end
  else
    ShowMessage('All fields are required.');
end;

procedure TfmCountry.DBGrid1DblClick(Sender: TObject);
begin
  txtAbbr.Text := DBGrid1.DataSource.DataSet.FieldByName('ABBRIVIATION').AsString;
  txtCountry.Text := DBGrid1.DataSource.DataSet.FieldByName('COUNTRY').AsString;
  Operation := opEdit;
end;

procedure TfmCountry.DBGrid1TitleClick(Column: TColumn);
begin
  fbCountry.IndexFieldNames := Column.FieldName;
end;

procedure TfmCountry.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  FreeAndNil(fmCountry);
end;

procedure TfmCountry.FormCreate(Sender: TObject);
begin
  Operation := opNew;
  btnRefresh.Click;
end;

end.
